﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using GameLibrary;
using Rescue_of_the_Dwarvenzombies.SceneObjects.GameObjects.Items;
using Rescue_of_the_Dwarvenzombies.SceneObjects.SceneObjectsHandlers;

namespace Rescue_of_the_Dwarvenzombies.SceneObjects.GameObjects.Characters.Enemies
{
    public class Enemy : Character
    {
        public float ItemDropRate { get; set; }
        public float Health { get; set; }
        public Item Possesion { get; set; }
        public int Score { get; set; }
        private List<IEnemyObserver> listeners = new List<IEnemyObserver>();
        private float timeSinceLastAttack = 0;

        private float fader;

        /// <summary>
        /// Initializes a new instance of the Enemy class.
        /// </summary>
        /// <param name="enemyHandler">The enemy handler.</param>
        /// <param name="itemHandler">The item handler.</param>
        /// <param name="position">The position.</param>
        public Enemy(IEnemyObserver enemyHandler, IEnemyObserver itemHandler, Vector2 position)
        {
            listeners.Add(enemyHandler);
            listeners.Add(itemHandler);
            Position = position;
            LastPosition = Position;
            fader = 0.000001f;
        }

        public override void Attack()
        {
        }

        /// <summary>
        /// Updates the the enemy
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        public override void Update(GameTime gameTime)
        {
            timeSinceLastAttack += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (fader < 1)
            {
                fader += (float)(gameTime.ElapsedGameTime.TotalSeconds);
                TintColor = Color.White * fader;
            }
            else
                TintColor = Color.White;
            Move();
        }

        /// <summary>
        /// Handle the move computed by the AI
        /// </summary>
        public override void Move()
        {
            WantedMove = AI.Instance().GetAIMove(Position) * Velocity;

            if ((WantedMove.X < 0.2 && WantedMove.X > -0.2) && WantedMove.Y < 0)
            {
                WantedAction = Constant.Action.MoveUp;
                Direction = Constant.Direction.Up;
            }
            else if ((WantedMove.X < 0.2 && WantedMove.X > -0.2) && WantedMove.Y > 0)
            {
                WantedAction = Constant.Action.MoveDown;
                Direction = Constant.Direction.Down;
            }
            else if (WantedMove.X < 0 && WantedMove.Y < 0)
            {
                WantedAction = Constant.Action.MoveUpLeft;
                Direction = Constant.Direction.UpLeft;
            }
            else if (WantedMove.X > 0 && WantedMove.Y < 0)
            {
                WantedAction = Constant.Action.MoveUpRight;
                Direction = Constant.Direction.UpRight;
            }
            else if (WantedMove.X < 0 && WantedMove.Y > 0)
            {
                WantedAction = Constant.Action.MoveDownLeft;
                Direction = Constant.Direction.DownLeft;
            }
            else if (WantedMove.X > 0 && WantedMove.Y > 0)
            {
                WantedAction = Constant.Action.MoveDownRight;
                Direction = Constant.Direction.DownRight;
            }
            else if (WantedMove.X < 0 && WantedMove.Y == 0)
            {
                WantedAction = Constant.Action.MoveLeft;
                Direction = Constant.Direction.Left;
            }
            else if (WantedMove.X > 0 && WantedMove.Y == 0)
            {
                WantedAction = Constant.Action.MoveRight;
                Direction = Constant.Direction.Right;
            }
        }

        /// <summary>
        /// Managing the damage taken by the enemy, and if
        /// the enemy die
        /// </summary>
        /// <param name="damage">The damage.</param>
        public override void TakeDamage(float damage)
        {
            Health -= damage;
            if (Health > 0) return;
            foreach (IEnemyObserver listener in listeners)
            {
                listener.IsDead(this);
            }
        }

        /// <summary>
        /// Gets the damage.
        /// </summary>
        /// <returns></returns>
        public override float GetDamage()
        {
            if (timeSinceLastAttack >= RateOfFire)
            {
                timeSinceLastAttack = 0;
                return base.GetDamage();
            }
            return 0;
        }
    }
}